//
// Created by shenhangye on 2025/3/3.
//

#ifndef VIDEOEDIT_AUDIOPLAYER_H
#define VIDEOEDIT_AUDIOPLAYER_H
#define VOL_FILE_PATH "/storage/emulated/0/test/new_test_pcm.pcm"
#include "SoundTouch.h"
#include "JavaCallHelper.h"
#include "BasePlayer.h"
#include <SLES/OpenSLES.h>
#include <LogUtil.h>
#include <SLES/OpenSLES_Android.h>
extern "C"{
#include<libavutil/time.h>
#include<libswresample/swresample.h>
#include "libavutil/opt.h"
};
class AudioPlayer : public  BasePlayer{
public:
    ~AudioPlayer() override;
    explicit AudioPlayer(FFStatus *ffstatus,int sample_rate,JavaCallHelper *helper);

    int play_t(void **pcmbuf);
    void play() override;
    int getSoundTouchData();
    static void *start(void *argu);
    int volume = 50;
    void initOpenSLES();
    //已播放时长
    double clock = 0;
    JavaCallHelper *javaCallHelper= nullptr;
    SLuint32 getCurrentSampleRateForOpensles(int sample_rale);
    int nb = 0;
    AVPacket *avPacket = nullptr;
    AVFrame *avFrame = nullptr;
    int ret = 0;
    //当前frame时间
    double now_frame_time = 0;

public:
    SLObjectItf engineObject = nullptr;
    SLEngineItf engineEngine = nullptr;
    uint8_t *buffer = nullptr;
    //混音器
    SLObjectItf outputMixObject = nullptr;
    SLEnvironmentalReverbItf outputMixEnvironmentalReverb = nullptr;
    SLEnvironmentalReverbSettings reverbSettings = SL_I3DL2_ENVIRONMENT_PRESET_STONECORRIDOR;

    //pcm
    SLObjectItf pcmPlayerObject = nullptr;
    SLPlayItf pcmPlayerPlay = nullptr;
    SLVolumeItf pcmPlayVolume = nullptr;

    //缓冲器队列接口
    SLAndroidSimpleBufferQueueItf pcmBufferQueue = nullptr;
    float amplifyVol = 5.0f;
    bool isReadFrameFinish = true;
    int sample_rate = 0;
    float pitch = 1.0f;
    float speed = 1.0f;
    int num = 0;
    FILE *fp_out = nullptr;
    soundtouch::SAMPLETYPE *sampleBuffer = nullptr;
    soundtouch::SoundTouch *soundTouch = nullptr;
    void setVolume(int volume);
    uint8_t *out_buffer = nullptr;
    bool finished = true;
    int data_size = 0;
    void OnFrameAvailable(AVFrame *frame) override;
    int volume_adjust(short  * in_buf,  float in_vol);


};


#endif //VIDEOEDIT_AUDIOPLAYER_H
